package com.youbar.client.impl;

import com.youbar.client.basic.BizResult;
import com.youbar.client.dao.UserMapper;
import com.youbar.client.dto.LoginDto;
import com.youbar.client.dto.user.RegisterDto;
import com.youbar.client.dto.user.StateDto;
import com.youbar.client.dto.user.UpdateInfoDto;
import com.youbar.client.enums.ResultCode;
import com.youbar.client.enums.State;
import com.youbar.client.method.HashUtil;
import com.youbar.client.model.User;
import com.youbar.client.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public BizResult register(RegisterDto registerDto) {
        // 判断密码是否一致
        if (!registerDto.getPassword().equals(registerDto.getPasswordAgain())){
            return BizResult.error(ResultCode.NOT_SAME);
        }
        // 判断用户是否注册
        User userFirst = userMapper.selectByAccount(registerDto.getMobile());
        if (userFirst != null) {
            return BizResult.error(ResultCode.USER_HAVE_EXIST);
        }
        User user = new User();
        user.setAccount(registerDto.getMobile());
        user.setMobile(registerDto.getMobile());
        user.setPassword(registerDto.getPassword());
        user.setNickName(registerDto.getMobile());
        user.setState(1);
        user.setRegTime(new Date());
        // 密码加密
        user.setPassword(HashUtil.md5(registerDto.getPassword()));
        System.out.print("注册信息" + user.toString());
        int i = userMapper.insertUser(user);
        if (i != 0) {
            return BizResult.success();
        }
        return BizResult.error();
    }

    @Override
    public BizResult forgotPwd(RegisterDto registerDto) {
        // 判断用户是否存在
        User user = userMapper.selectByAccount(registerDto.getMobile());
        if (user == null) {
            return BizResult.error(ResultCode.USER_NOT_EXIST);
        }
        // 判断密码是否一致
        if (!registerDto.getPassword().equals(registerDto.getPasswordAgain())){
            return BizResult.error(ResultCode.NOT_SAME);
        }
        user.setPassword(registerDto.getPassword());
        // 密码加密
        user.setPassword(HashUtil.md5(registerDto.getPassword()));
        int i = userMapper.updatePassword(user);
        if (i != 0) {
            return BizResult.success();
        }
        return BizResult.error();
    }

    @Override
    public BizResult login(LoginDto loginDto) {
        // 判断用户是否存在
        User user = userMapper.selectByAccount(loginDto.getAccount());
        if (user == null) {
            return BizResult.error(ResultCode.USER_NOT_EXIST);
        }
        if (State.Account.FROZEN.ordinal() == user.getState()){
            return BizResult.error(ResultCode.USER_FREEZE);
        }
        if (State.Account.DELETE.ordinal() == user.getState()){
            return BizResult.error(ResultCode.USER_NOT_EXIST);
        }
        if (!HashUtil.md5(loginDto.getPassword()).equals(user.getPassword())) {
            return BizResult.error(ResultCode.USER_PASS_ERROR);
        }
        user.setLastLoginTime(new Date());
        userMapper.login(user);
        user.setPassword(null);
        return BizResult.success(user);
    }

    @Override
    public BizResult getInfo(int id) {
        return BizResult.success(userMapper.getInfo(id));
    }

    @Override
    public BizResult updateInfo(UpdateInfoDto infoDto) {
        User user = new User();
        BeanUtils.copyProperties(infoDto, user);
        int i = userMapper.updateUser(user);
        if (i == 0) return BizResult.error();
        return BizResult.success();
    }

    @Override
    public BizResult updateState(StateDto stateDto) {
        User user = new User();
        BeanUtils.copyProperties(stateDto, user);
        System.out.print("修改用户状态" + user.getState());
        int i = userMapper.updateState(user);
        if (i == 0) return BizResult.error();
        return BizResult.success();
    }

    @Override
    public BizResult deleteUser(int id) {
        int i = userMapper.deleteUser(id);
        if (i == 0) return BizResult.error();
        return BizResult.success();
    }
}
